//
//  JKRFeedContentLayout.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/10.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedContentLayout.h"

@implementation JKRFeedContentLayout

- (instancetype)initWithModel:(JKRFeedDataModel *)model {
    self = [super init];
    
    if (model.order.order_type == 1) { // 陪玩
        switch (model.order.order_status) {
            case 1: // 已发布
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else { // 游客
                    
                }
            }
                break;
            case 4: // 超时无人购买
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else { // 游客
                    
                }
            }
                break;
            case 6: // 老板买单
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    
                } else { // 游客
                    
                }
            }
                break;
            case 7: // 超时未填写结果
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    
                } else { // 游客
                    
                }
            }
                break;
            case 8: // 填写结果-打了
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    
                } else { // 游客
                    
                }
            }
                break;
            case 9: // 填写结果-没打
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    
                } else { // 游客
                    
                }
            }
                break;
            case 10: // 填写打了超时未确认或者确认服务
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    
                } else { // 游客
                    
                }
            }
                break;
            case 11: // 发起投诉
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    
                } else { // 游客
                    
                }
            }
                break;
            case 12: // 申诉成功
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    
                } else { // 游客
                    
                }
            }
                break;
            case 13: // 申诉失败
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    
                } else { // 游客
                    
                }
            }
                break;
            default:
                break;
        }
    } else if (model.order.order_type == 2) { // 约玩
        switch (model.order.order_status) {
            case 1: // 已发布
            {
                BOOL isSignUp = NO;
                for (JKRFeedOrderCandidate *candidate in model.order.candidates) {
                    if ([candidate.uid isEqualToString:CurrentUserID]) {
                        isSignUp = YES;
                        break;
                    }
                }
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if(isSignUp) { // 报名者
                    
                } else { // 游客
                    
                }
            }
                break;
            case 2: // 超时无人报名
            {
                BOOL isSignUp = NO;
                for (JKRFeedOrderCandidate *candidate in model.order.candidates) {
                    if ([candidate.uid isEqualToString:CurrentUserID]) {
                        isSignUp = YES;
                        break;
                    }
                }
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if(isSignUp) { // 报名者
                    
                } else { // 游客
                    
                }
            }
                break;
            case 3: // 超时未选大神
            {
                BOOL isSignUp = NO;
                for (JKRFeedOrderCandidate *candidate in model.order.candidates) {
                    if ([candidate.uid isEqualToString:CurrentUserID]) {
                        isSignUp = YES;
                        break;
                    }
                }
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if(isSignUp) { // 报名者
                    
                } else { // 游客
                    
                }
            }
                break;
            case 5: // 选择大神
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    
                } else { // 游客
                    
                }
            }
                break;
            case 7: // 超时未填写结果
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    
                } else { // 游客
                    
                }
            }
                break;
            case 8: // 填写结果-打了
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    
                } else { // 游客
                    
                }
            }
                break;
            case 9: // 填写结果-没打
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    
                } else { // 游客
                    
                }
            }
                break;
            case 10: // 填写打了超时未确认或者确认服务
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    
                } else { // 游客
                    
                }
            }
                break;
            case 11: // 发起投诉
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    
                } else { // 游客
                    
                }
            }
                break;
            case 12: // 申诉成功
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    
                } else { // 游客
                    
                }
            }
                break;
            case 13: // 申诉失败
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    
                } else { // 游客
                    
                }
            }
                break;
            default:
                break;
        }
        
    }
    
    self.imageURLs = [NSMutableArray array];
    self.imageFrames = [NSMutableArray array];
    NSMutableAttributedString *contentString = [[NSMutableAttributedString alloc] initWithString:model.feed.content];
    contentString.font = [UIFont systemFontOfSize:14];
    contentString.color = UIColorHex(444444);
    contentString.lineBreakMode = NSLineBreakByCharWrapping;
    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth - 15 - 9, MAXFLOAT)];
    self.contentLabelLayout = [YYTextLayout layoutWithContainer:container text:contentString];
    self.contentLabelFrame = CGRectMake(15, 0, self.contentLabelLayout.textBoundingRect.size.width, self.contentLabelLayout.textBoundingRect.size.height);
    
    NSInteger imageCount = model.feed.images.count;
    if (imageCount == 0) {
        self.contentFrame = CGRectMake(0, 0, kScreenWidth, CGRectGetMaxY(self.contentLabelFrame) + 15);
        return self;
    }
    
    CGFloat imagePaddingLeft = 16.f;
    CGFloat imagePaddingRight = 12.f;
    CGFloat imagePaddingTop = 10.f + CGRectGetMaxY(self.contentLabelFrame);
    CGFloat imageMargin = 5.f;
    CGFloat imageWH;
    if (imageCount == 1) {
        imageWH = (kScreenWidth - imagePaddingLeft - imagePaddingRight) * 0.6;
    } else if (imageCount == 2) {
        imageWH = (kScreenWidth - imagePaddingLeft - imagePaddingRight - imageMargin) * 0.5;
    } else {
        imageWH = (kScreenWidth - imagePaddingLeft - imagePaddingRight - imageMargin * 3) / 4.0;
    }
    for (NSInteger i = 0; i < 9; i++) {
        CGFloat imageX;
        CGFloat imageY;
        CGFloat imageW;
        CGFloat imageH;
        if (i < imageCount) {
            imageW = imageWH;
            imageH = imageWH;
            imageX = imagePaddingLeft + (i % 4) * (imageW + imageMargin);
            imageY = imagePaddingTop + (i / 4) * (imageH + imageMargin);
            CGRect imageFrame = CGRectMake(imageX, imageY, imageW, imageH);
            NSURL *imageURL = [NSURL URLWithString:model.feed.images[i]];
            [self.imageURLs addObject:imageURL];
            [self.imageFrames addObject:[NSValue valueWithCGRect:imageFrame]];
        } else {
            break;
        }
    }
    self.contentFrame = CGRectMake(0, 0, kScreenWidth, CGRectGetMaxY(self.imageFrames.lastObject.CGRectValue) + 15);
    return self;
}

@end
